package com.example.utils;



import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Component
public class EmailUtil {



    public static void main(String[] args) {
        //发件人邮箱
        String from = "liu999549@163.com";
        //发件人邮箱密码
        final String password = "Lhy112233...";
        //收件人邮箱地址
        String to = "2696907441@qq.com";
        // 设置邮件服务器属性
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");

        // 创建会话对象
        Session session = Session.getInstance(properties,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(from, password);
                    }
                });
        try {
            // 创建邮件消息
            Message message = new MimeMessage(session);
            // 设置发件人
            message.setFrom(new InternetAddress(from));
            // 设置收件人
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));
            // 设置邮件主题
            message.setSubject("学生成绩");
            // 设置邮件正文
            message.setText("This is a test email sent from Java.");

            // 发送邮件
            Transport.send(message);

            System.out.println("Email sent successfully.");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

}
